package com.dnxh.controller;

import cn.hutool.core.codec.Base64;
import com.dnxh.util.ImageUtil;
import com.dnxh.util.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

/**
 * The type Common controller.
 *
 * @Author 枳湫
 * @Time 2022 /4/8 11:24
 * @Describe Basic_jar
 */
@Slf4j
@Controller
@RequestMapping ("/common")
public class CommonController {
	
	@Value ("${server.port}")
	private int                 port;
	@Resource
	private HttpServletResponse httpServletResponse;
	
	/**
	 * Fill in data string.
	 *
	 * @return the string
	 */
	@GetMapping ("/basic")
	public String fillInData () {
		return "FillIn";
	}
	
	/**
	 * Search meeting url.
	 *
	 * @param id the id
	 */
	@GetMapping ("/meeting/{id}")
	public void searchMeetingUrl (@PathVariable ("id") Long id) {
		String ip = IpUtil.getOutIpV4 ();
		String url = ip + ":" + port + "/common/basic?" + Base64.encode ("id=" + id);
		log.info ("获取到的URL:{}", url);
		try (OutputStream outputStream = httpServletResponse.getOutputStream ()) {
			ImageUtil.generateImage (outputStream,url);
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}
	
	/**
	 * 发送二维码图片
	 *
	 * @param data the data
	 */
	@GetMapping("/email/qr")
	public void sendEmailQr(@RequestParam("data") String data){
		String str = Base64.decodeStr (data);
		long id = Long.parseLong (str);
		String url = IpUtil.getOutIpV4 () + ":" + port + "/basicMeeting/check/" + id;
		log.info ("获取到的URL:{}", url);
		try (OutputStream outputStream = httpServletResponse.getOutputStream ()) {
			ImageUtil.generateImage (outputStream,url);
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}
}
